<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="课题名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入课题名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['project:topic:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>


    <el-table v-loading="loading" :data="topicList" @selection-change="handleSelectionChange">
      <!-- <el-table-column type="selection" width="55" align="center"/> -->
      <el-table-column label="编号" align="center" prop="id" width="50px"/>
      <el-table-column label="课题名" align="center" prop="name" :min-width="getContentWidth('name')"
                       :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <div class="ellipsis">{{ scope.row.name }}</div>
        </template>
      </el-table-column>
      <el-table-column label="线条" align="center" prop="line" width="80">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.project_line" :value="scope.row.line"/>
        </template>
      </el-table-column>
      <el-table-column label="人员状态" align="center" prop="isFull" width="80">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.is_full" :value="scope.row.isFull"/>
        </template>
      </el-table-column>
      <el-table-column label="浏览量" align="center" prop="clickNum" width="80"/>
      <el-table-column label="需求人数" align="center" prop="needNum" width="100"/>
      <el-table-column label="创建时间" align="center" prop="createTime" width="150">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="topicDetailsClick(scope.row)"

          >查看详情
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleJoinDetail(scope.row)"
          >申请加入团队
          </el-button>
        </template>
      </el-table-column>
    </el-table>


    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <el-dialog :title="title" :visible.sync="detailOpen" width="780px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="需求名:">{{ form.name }}</el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="线条:">{{ form.line }} / {{ lineFormat(form) }}</el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="内容:">
              <div v-html="form.description"></div>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="审核人1:">{{ form.checker1Name }}</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="审核人2:">{{ form.checker2Name }}</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="创建时间:">{{ form.createTime }}</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="需求人数:">{{ form.needNum }}</el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">关闭</el-button>
      </div>
    </el-dialog>


    <!--申请加入团队-->
    <el-dialog :title="title" :visible.sync="joinOpen" width="780px" append-to-body>
      <el-form ref="joinForm" :model="joinForm" :rules="joinRules" label-width="100px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="个人介绍" prop="description">
              <editor v-model="joinForm.description" :min-height="192"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-col :span="24">
          <el-form-item label="申请原因" prop="reason">
            <el-input v-model="joinForm.reason" type="textarea" placeholder="请输入原因"/>
          </el-form-item>
        </el-col>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitJoinForm">确 定</el-button>
        <el-button @click="cancel">关闭</el-button>
      </div>
    </el-dialog>


    <!-- 添加或修改课题对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="780px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="课题名:" prop="name">
          <el-input v-model="form.name" placeholder="请输入课题名"/>
        </el-form-item>
        <el-row>
          <el-col :span="12">
            <el-form-item label="线条:" prop="line" label-width="100px">
              <el-select v-model="form.line" placeholder="线条" clearable>
                <el-option
                  v-for="dict in dict.type.project_line"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="课题人数:" prop="needNum">
              <el-input-number v-model="form.needNum" controls-position="right" :min="3"/>
              <div class="note">团队人数最少3人</div>
            </el-form-item>
          </el-col>
        </el-row>

        <!-- 多选框标题 -->
        <el-form-item label="重点工作:" prop="keyTasks">
          <!-- 多选框 -->
          <div>
            <label v-for="(item, index) in options" :key="index">
              <input type="checkbox" :value="item" v-model="selectedOptions">
              {{ item }}
            </label>
            <div v-if="selectedOptions.length > 3" style="color: red;">最多只能选择三项</div>
          </div>
        </el-form-item>

        <el-form-item label="课题描述:" prop="description">
          <editor v-model="form.description" :min-height="200"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listTopic, getTopic, delTopic, addTopic, updateTopic, listTopicByClickNum} from "@/api/project/topic";
import {addJoinForm} from "@/api/project/checkList";
import {listAchievementByClickNum} from "@/api/project/achievement";

export default {
  name: "topic",
  dicts: ['project_state', 'project_line', 'team_name', 'is_full'],
  data() {
    return {
      options: [
        "5G领先",
        "服务升级",
        "产业链联合",
        "耕云行动",
        "科技创新",
        "绿色低碳",
        "流程风控",
        "网络服务智能化",
        "网格运营",
        "网络安全",
        "人才强企",
        "建强算力",
        "智慧营销",
        "乡村振兴",
        "宽带运营",
        "品牌运营",
        "智慧家庭",
        "党建创新",
        "中台与数据",
        "VR融合运营",
        "员工关爱",
        "政企增收",
        "权益生态",
        "云家庭",
        "8+2战略",
        "产品优化",
        "组织体系",
        "科学管理",
        "产品运营",
        "合作生态",
        "云办公",
        "重点领域改革",
        "生产安全",
        "降本增效"
      ],
      selectedOptions: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 课题表格数据
      topicList: [],
      // 弹出层标题
      title: "",
      detailOpen: false,
      joinOpen: false,
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        type: 2,
        line: null,
        prize: null,
        awardTime: null,
        userId: null,
        otherPrize: null,
        state: null,
        needNum: null,
        // isShow: 1
      },
      // 表单参数
      form: {},
      joinForm: {},
      joinRules: {
        description: [
          {required: true, message: "个人介绍不能为空", trigger: "blur"}
        ], reason: [
          {required: true, message: "申请理由不能为空", trigger: "blur"},
          {min: 10, max: 100, message: '理由长度必须介于 10 和 100 之间', trigger: 'blur'}
        ]
      },
      // 表单校验
      rules: {
        name: [
          {required: true, message: "课题名称不能为空", trigger: "blur"},
          {min: 2, max: 40, message: '课题名称长度必须介于 2 和 40 之间', trigger: 'blur'}
        ],
        line: [
          {required: true, message: "线条不能为空", trigger: "blur"}
        ],
        needNum: [
          {required: true, message: "需求人数不能为空", trigger: "blur"}
        ],
        description: [
          {required: true, message: "描述不能为空", trigger: "blur"}
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    // 线条字典类型翻译
    lineFormat(row, column) {
      return this.selectDictLabel(this.dict.type.project_line, row.line);
    },
    // 操作人1
    check1Format(row, column) {
      return this.selectDictLabel(this.dict.type.team_name, row.checker1);
    },
    // 操作人2
    check2Format(row, column) {
      return this.selectDictLabel(this.dict.type.team_name, row.checker2);
    },
    getContentWidth(prop) {
      // 根据数据内容动态计算每列的最小宽度，确保内容不被压缩
      // 可以根据实际情况调整计算逻辑，比如考虑内容长度、字体大小等
      return '100px'; // 默认值
    },
    /** 查询课题列表 */
    getList() {
      this.loading = true;
      listTopicByClickNum(this.queryParams).then(response => {
        this.topicList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.detailOpen = false;
      this.joinOpen = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        name: undefined,
        type: 3,
        description: undefined,
        line: undefined,
        keyTask: undefined,
        keyTask1: undefined,
        keyTask2: undefined,
        keyTask3: undefined,
        prize: undefined,
        awardTime: undefined,
        file: undefined,
        innovation: undefined,
        userId: undefined,
        clickNum: 0,
        otherPrize: undefined,
        checker1: undefined,
        checker2: undefined,
        state: 0,
        needNum: undefined,
        createTime: undefined
      };
      this.joinForm = {
        description: undefined,
        reason: undefined,
      }
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    topicDetailsClick(row) {
      this.$router.push({path : '/topicDetail',query:{"id" : row.id, "from" : 1}});
    },
    handleJoinDetail(row){
      this.reset();
      const id = row.id || this.ids
      getTopic(id).then(response => {
        this.form = response.data;
        this.joinOpen = true;
        this.title = "申请加入《"+ this.form.name+"》团队";
      });
    },

    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加课题";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getTopic(id).then(response => {
        /*        this.options.forEach(option => {
                  if (row.keyTask.indexOf(option) !== -1) {
                    this.selectedOptions.push(option);
                  }
                });*/
        this.form = response.data;
        this.open = true;
        this.title = "修改课题";
      });
    },

    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (this.selectedOptions.length > 3) {
          this.$message.error('最多只能选择三项重点工作');
        } else if (valid) {
          const selectedTasks = this.selectedOptions.slice(0, 3);
          this.form.keyTask = selectedTasks;
          if (this.form.id != null) {
            updateTopic(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addTopic(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },

    /** 提交按钮 */
    submitJoinForm() {
      this.$refs["joinForm"].validate(valid => {
        if (valid) {
          this.joinForm = this.form;
          addJoinForm(this.joinForm).then(response => {
            this.$modal.msgSuccess("提交申请成功");
            this.joinOpen = false;
            this.getList();
          })
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除课题编号为"' + ids + '"的数据项？').then(function () {
        return delTopic(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('project/topic/export', {
        ...this.queryParams
      }, `topic_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>

<style scoped>
/* 样式调整 */
.dialog-footer {
  text-align: right;
  padding-top: 10px;
}

/* 课题人数的备注样式 */
.note {
  font-size: 12px;
  margin-top: 5px;
  color: #999;
}
</style>
